<?php
namespace app\user\controller;

use \think\Controller;
use think\Request;
use think\Session;
use think\Hook;
use app\user\traits\controllers\TraitUser;
use app\common\controller\Api;
/**
 * 会员房车资料
 */
class CaravanInfo extends Api
{
	use \app\user\traits\models\UserInfoCheck;
    // use \app\common\traits\controller\TraitController;
	use \app\common\traits\controller\TraitUpload;
    use TraitUser;

	//前置操作
    protected $beforeActionList = [
        'checkLogin',
        'checkUserInfo'
    ];
    /**
     * [checkLogin 判断是否登陆]
     * @return [type] [description]
     */
    protected function checkLogin()
    {
        if (!model('Users')->checkLogin()) {
            Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect('@user/login');
            exit;
        }
    }
    //判断是否完善必填项 real_name/email
    public function checkUserInfo()
    {
        $bt1 = model('Users')->getLoginUserInfo('real_name');
        $bt2 = model('Users')->getLoginUserInfo('email');
        if (empty($bt1)||empty($bt2)) {
            $this->redirect('@userinfo/personinfo');
        }
    }
    //地区插件
    /**
     * [getCountry 国家]
     * @return [type] [description]
     */
    public function getCountry()
    {
        $country = db('area')->where('type',0)->field(['area_code','parent_code','area_name','type'])->select();
        return json($country);
    }
    /**
     * [getProvince 省份]
     * @return [type] [description]
     */
    public function getProvince()
    {
        $addressProvince = input('addressCountry');
        $province = db('area')->where('type',1)->where('parent_code',$addressProvince)->field(['area_code','parent_code','area_name','type'])->select();
        return  json($province);
    }
    /**
     * [getCity 城市]
     * @return [type] [description]
     */
    public function getCity()
    {
       
        $city = input('addressProvince');
        $city = db('area')->where('type',2)->where('parent_code',$city)->field(['area_code','parent_code','area_name','type'])->select();
        return json($city);
    }
    /**
     * [getCamp 营地]申明'area_code'=>-99，为其他营地，即该城市没有营地，填写的营地。
     * 后台可以根据查找camp_id=-99,收集信息。
     * @return [type] [description]
     */
    public function getCamp()
    {
        $city = input('addressCity');
        //取审核通过的营地
        $camp = db('camps')->where('city',$city)->where('status',1)->field(['id as area_code','count as parent_code','name as area_name','type'])->select();
        $camp[] = ['area_code'=>-99,'parent_code'=>'','area_name'=>'其他','type'=>''];
        return json($camp);
    }
	/**
	 * [getPersonInfo 房车信息界面]
	 * @return [type] [description]
	 */
	public function getindex()
	{
        //绑定房车-行为
        Hook::exec('app\\behavior\\Base','checkbindcard');
        $user_id = session('user.id');
        $caravaninfo = model('CaravanInfos')->findCaravan($user_id);
        // $registration = $caravaninfo->registration;
        // $status = model('Users')->getLoginUserInfo('status');
        // halt($status);
		$ziduan1 = model('CaravanInfos')->getZiduan1();
		$ziduan2 = model('CaravanInfos')->getZiduan2();
		$ziduan3 = model('CaravanInfos')->getZiduan3();
		$user_id = session('user.id');
		$user_type = model('Users')->getLoginUserInfo('status');//用户是否绑定成功1成功，默认0。
		$cates = db('car_cates')->column('name', 'id');//房车类型
		$this->assign('cates',$cates);
        $brands = db('car_brands')->column('name', 'id');//房车品牌
        $this->assign('brands',$brands);
        $countries = db('car_countries')->column('name', 'id');//房车国别
        $this->assign('countries',$countries);
        $dr_type = db('driver_types')->column('name', 'id');//准驾类型
        $this->assign('dr_type',$dr_type);
        $tg = 1;//绑定成功
        $this->assign('tg',$tg);
		$list = $caravaninfo->toArray();
        if ($b = $this->old()) {
            $list = array_merge($list,$b);
        }
		return view('user/caravaninfo',['ziduan2'=>$ziduan2,'ziduan1'=>$ziduan1,'list'=>$list,'user_type'=>$user_type,'ziduan3'=>$ziduan3]);
	}
	/**
	 * [postUpdateInfoForm 房车信息表单]
	 * @return [type] [description]
	 */
	public function postUpdateInfoForm()
	{
        //缓存记录
        $this->flash();
        $post_caravan_info['name'] = input('post.name');
		$post_caravan_info['speed'] = input('post.speed');
        //检测空字段
        $user_type = model('Users')->getLoginUserInfo('status');
        //$user_type =2;
        $user_id = session('user.id');
        if ($user_type<>1) {
        	$check_info['registration'] = input('post.registration');//交车日期
			//$check_info['size'] = input('post.size');//房车尺寸去掉
			$check_info['people_num'] = input('post.people_num');
			$check_info['single_bed_num'] = input('post.single_bed_num');
	        $check_info['double_bed_num'] = input('post.double_bed_num');
	        $check_info['sofe_num'] = input('post.sofe_num');
	        $re = $this->getCheckNull($check_info); 
	        if (!$re) {
        	   return $this->error('请完善必填项',$url = null,'',$wait=5);
        	}
        	//必填字段
        	$post_caravan_info['car_cates_id'] = input('post.car_cates_id');
        	$post_caravan_info['car_countries_id'] = input('post.car_countries_id');
			$post_caravan_info['car_brands_id'] = input('post.car_brands_id');
			$post_caravan_info['driver_type'] = input('post.driver_type');
			$post_caravan_info['registration'] = input('post.registration'); 
            if (!empty(input('post.addressCamp'))) {
                //营地id
                $post_caravan_info['services'] = input('post.addressCamp');
            }
			//$post_caravan_info['size'] = input('post.size');//房车尺寸去掉
			$post_caravan_info['single_bed_num'] = input('post.single_bed_num');
			$post_caravan_info['double_bed_num'] = input('post.double_bed_num');
			$post_caravan_info['people_num'] = input('post.people_num');
			$post_caravan_info['sofe_num'] = input('post.sofe_num');
			$post_caravan_info['kitchen'] = input('post.kitchen');
			$post_caravan_info['bathroom'] = input('post.bathroom');
            //地区-国家、省份、城市
            $post_caravan_info['country'] = input('post.addressCountry');
            $post_caravan_info['province'] = input('post.addressProvince');
            $post_caravan_info['city'] = input('post.addressCity');
            //其他营地
            if (input('post.addressCamp')==-99) {
                if (empty(input('post.services_other'))) {
                   return $this->error('请填写其他房车地址',$url = null,'',$wait=5);
                }
            $post_caravan_info['services_other'] = input('post.services_other');
            }

        }
        //user表数据
        $post_caravan_info['park_num'] = input('post.park_num');
        if (!empty($post_caravan_info['services'])) {
            
                model('Users')->where('id',$user_id)->setField('camp_id',input('post.addressCamp'));
            } 
        if (!empty($post_caravan_info['park_num'])) {
                model('Users')->where('id',$user_id)->setField('park_num',$post_caravan_info['park_num']);
            }      
		$post_caravan_info['wifi'] = input('post.wifi');
        $post_caravan_info['tv'] = input('post.tv');
        $post_caravan_info['washer'] = input('post.washer');
		$post_caravan_info['air_condition'] = input('post.air_condition');
		$post_caravan_info['the_shower'] = input('post.the_shower');
        $post_caravan_info['extinguisher'] = input('post.extinguisher');
        $post_caravan_info['annunciator'] = input('post.annunciator');
		$post_caravan_info['monitored'] = input('post.monitored');
		//车主姓名
		$post_caravan_info['owner_name'] = model('Users')->getLoginUserInfo('real_name');
        $post_caravan_info['brand_number'] = input('post.brand_number');
		$post_caravan_info['user_id'] = session('user.id');
        //房车编号
        $post_caravan_info['number'] = model("CaravanInfos")->quickRandom(12);
		//图片处理判断是否上传
        if (!empty(input('post.caravanphoto/a'))) {
            $post_caravan_info['piccaravan'] = input('post.caravanphoto/a');
        }
        if (!empty(input('post.caravancover/a'))) {
            $post_caravan_info['caravancover'] = input('post.caravancover/a');
        }
        //分公司id与区域
        if (!empty(input('post.addressCamp'))&&input('post.addressCamp')<>-99) {
            $campinfo = model('Camps')->where('id',input('post.addressCamp'))->find();
            $post_caravan_info['branch_id'] = $campinfo->branch_id;
            $post_caravan_info['area'] = $campinfo->area;
            model('Users')->where('id',$user_id)->setField('camp_name',$campinfo->name);
        }
		//halt($post_caravan_info);
        $res = model('CaravanInfos')->updateCaravan($user_id,$post_caravan_info);
        if ($res) {
            //拒绝恢复状态    
            if ($user_type==-99) {
                    model('CaravanInfos')->where('id',$user_id)->setField('status',0);
                    model('Users')->where('id',$user_id)->setField('status',0);
            }

        	return $this->success(lang('提交成功'),'/caravaninfo/index');
        }else{
        	return $this->error(lang('提交失败'));
        }
	}
	/**
	 * [getCheckNickName 检测空字段]
	 * @return [type] []
	 */
	public function getCheckNull($info_array)
	{
		foreach ($info_array as $key => $value) {
            if ($value==0) {
                break;
            }
			if (empty($value)) {
				return false;
			}
		}
		return true;
	}

	public function postUploadImg()
	{
		if ($this->upload('file')) {
    		$url = $this->getUploadInfos();
    		return json(array(
                "state" => 'SUCCESS',
                "url" => $url['url'],
            ));
    	}
	}
    //////////////////////////审核三部曲/////////////////////////
    //正在审核===0
    public function getWaitCheck()
    {
        $userinfo = model('Users')->getLoginUserInfo();
        $user_id = model("Users")->getLoginUserInfo('id');
        $car_info = model('CaravanInfos')->where('user_id',$user_id)->find();
        $status = $car_info->status;
        if ($status==1) {
            $this->redirect('/caravaninfo/index');
            exit;
        }
        return view('caravanaudit/waitcheck',['car_info'=>$car_info,'userinfo'=>$userinfo]);
    }
    //审核通过===1
    public function getFinishCheck()
    {
        return view('caravanaudit/finishcheck');
    }
	//审核未通过====-99
    public function getFinishNoCheck()
    {
        return view('caravanaudit/finishnocheck');
    }
    /**
     * [getJumpWhich 跳转界面]
     * @param  [type] $status [description]
     * @return [type]         [description]
     */
    public function getJumpWhich($status)
    {
        if ($status == 1) {
            $this->redirect('/caravaninfo/finishcheck');
            exit;
        }elseif ($status == -99) {
            $this->redirect('/caravaninfo/finishnocheck');
            exit;
        }else{
            $this->redirect('/caravaninfo/waitcheck');
            exit;
        }
    }
}
